package com.sloan.community.repository;

import com.sloan.community.common.dto.CommentCountDTO;
import com.sloan.community.po.Comment;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.r2dbc.repository.R2dbcRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

@Repository
public interface CommentRepository extends R2dbcRepository<Comment, Long> {

    @Query("update t_comment set like_count = like_count + 1 where id =?commentId ")
    Mono<Void> likeCountById(@Param("commentId") Long commentId);

    @Query("update t_comment set like_count = like_count - 1 where id =?commentId and like_count >0 ")
    Mono<Void> unLikeCountById(@Param("commentId") Long commentId);

    Flux<Comment> findByArticleId(Long articleId);

    @Query("""
    SELECT
    	article_id,
    	COUNT( id ) AS comment_count 
    FROM
    	t_comment
    WHERE
    	article_id IN (:articleIds)
    GROUP BY
        article_id;
    """)
    Flux<CommentCountDTO> countByArticleIds(@Param("articleIds") List<Long> articleIds);
}
